package com.enjoy.studyfragment.fragment;

import android.content.Context;
import android.os.Vibrator;

import com.dou361.dialogui.DialogUIUtils;
import com.enjoy.studyfragment.R;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

/**
 * Created by Administrator on 2018/3/19.
 */

public class QRcodeFragment extends TopBarFragment {
    @BindView(R.id.zxingview)
    ZXingView mQRCodeView;

    @Override
    public Object setLayout() {
        return R.layout.fragment_qrcode;
    }

    @Override
    public void init() {
        super.init();
        mQRCodeView.setDelegate(new QRCodeView.Delegate(){

            @Override
            public void onScanQRCodeSuccess(String result) {
                Logger.d("onScanQRCodeSuccess:"+result);
                DialogUIUtils.showToast(result);
                vibrate();
                mQRCodeView.startSpot();//持续扫码
            }

            @Override
            public void onScanQRCodeOpenCameraError() {
                Logger.d("onScanQRCodeOpenCameraError:");
                DialogUIUtils.showToast("扫码失败，请重新扫码");
                mQRCodeView.startSpot();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mQRCodeView.startCamera();
        mQRCodeView.showScanRect();
        /**
         * 不添加下行代码，扫码界面黑屏
         */
        mQRCodeView.startSpot();
    }


    @Override
    public void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    public void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }
}
